package com.xf.bean;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.xf.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @ClassName ConsumerComponent
 * @Deseription TODO
 * @Author fx
 * @Date 2021/4/27 0027 21:54
 * @Version 1.0
 **/
@Component
public class ConsumerComponent {

    @Reference
    private HelloService helloService;

    private int i = 0;

    private static int coreSize = Runtime.getRuntime().availableProcessors();

    private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("hello-pool-%d").build();

    private static ExecutorService pool = new ThreadPoolExecutor(coreSize, coreSize * 2
            , 0L, TimeUnit.SECONDS, new LinkedBlockingDeque<>(10), THREAD_FACTORY, new ThreadPoolExecutor.DiscardOldestPolicy());

    public String sayHelloA(String name){
        return helloService.sayHelloA(name);
    }

    public String sayHelloB(String name) {
        return helloService.sayHelloB(name);
    }

    public String sayHelloC(String name) {
        return helloService.sayHelloC(name);
    }

    public void sayHello(String name, long startTime) {
        pool.submit(() -> {
            sayHelloA(name);
            sayHelloB(name);
            sayHelloC(name);
        });
        i++;
        if (i == 2000) {
            long sTime = System.currentTimeMillis();
            long time = sTime - startTime;
            System.out.println("2000次时间为：" + time + "ms");
        }
    }
}
